package com.nuoyun.tnvmue.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nuoyun.tnvmue.R;
import com.nuoyun.tnvmue.model.NewsModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangyao
 * Date: 2020/4/10
 * Email: jiangmr@vip.qq.com
 * TODO: 帮助列表适配器
 */
public class NewsAdapter extends BaseAdapter {

    Activity activity;

    public NewsAdapter(Activity activity) {
        this.activity = activity;
    }


    private List<NewsModel> models = new ArrayList<>();

    public void setModels(List<NewsModel> models) {
        this.models = models;
    }

    @Override
    public int getCount() {
        return models.size();
    }

    @Override
    public NewsModel getItem(int i) {
        return models.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyView myView;
        if (view == null) {
            myView = new MyView();
            view = View.inflate(activity, R.layout.item_news, null);
            myView.title = view.findViewById(R.id.title);
            myView.name = view.findViewById(R.id.name);
            myView.message = view.findViewById(R.id.message);
            view.setTag(myView);
        } else {
            myView = (MyView) view.getTag();
        }

        myView.title.setText(getItem(i).getTitle());
        myView.name.setText(getItem(i).getName());
        myView.message.setText(getItem(i).getMessage());

        return view;
    }

    static class MyView {
        TextView title,name,message;
        ImageView head;
    }
}
